//////////////////////////////////////////////////////////////////////////////////////////
// NAME: FileOperations.java								            				//
// AUTHOR: CDT Peter Lofreddo															//
// LAST MODIFIED: 20 March 2012															//
// DESCRIPTION: Contains methods for File IO for Nullwall 								//
// LIMITATIONS: 																		//
// CITATIONS: Inside Individual Methods.												//
//																						//
////////////////////////////////////////////////////////////////////////////////////////// 
package prototype.revision;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;

public class FileOperations {
	private TextView tv;
	private int numberLinesInBlack = 0;
	private int numberLinesInWhite = 0;
	private int numberLinesInPrograms;
	private int numberLinesInActive = 0;
	private Activity currentActivity;
	private File pwd;
	private String whiteList = "white.txt";
	private String blackList = "black.txt";
	private String installedPackages = "installed_programs.txt";

	public FileOperations(Activity act) {
		currentActivity = act;
		InitializeNullwall();
	}

	public FileOperations() {
		pwd = new File(Environment.getExternalStorageDirectory(), "Nullwall");
		InitializeNullwall();
	}

	public int getNumberLinesInFile(String fileName) {
		// ////////////////////////////////////////////////////////////////////////////////////////
		// NAME: getNumberLinesInFile(String fileName)
		// AUTHOR: CDT Riley Mckenna, CDT Peter Lofreddo
		// LAST MODIFIED: 20 March 2012
		// DESCRIPTION: Returns the number of lines in the specified filename.
		// LIMITATIONS: Must be one of the specified text files
		// CITATIONS:
		//
		// ////////////////////////////////////////////////////////////////////////////////////////
		String TAG = "File_OpgetNumberLinesInFile";
		if (fileName == blackList) {
			return numberLinesInBlack;
		} else if (fileName == whiteList) {
			return numberLinesInWhite;
		} else if (fileName == installedPackages) {
			return numberLinesInPrograms;
		} else if (fileName == "active_processes.txt") {
			return numberLinesInActive;
		} else {
			Log.e(TAG, "Not a valid Filename, Error in " + TAG);
			return 0;
		}

	}

	public File getFile() {
		return pwd;
	}

	public void InitializeNullwall()
	// ////////////////////////////////////////////////////////////////////////////////////////
	// NAME: InitializeNullwall()
	// AUTHOR: CDT Peter Lofreddo
	// LAST MODIFIED: 27 February 2012
	// DESCRIPTION: Initializes Catalog System.
	// LIMITATIONS:
	// CITATIONS:
	//
	// ////////////////////////////////////////////////////////////////////////////////////////
	{
		String TAG = "InitializeNullwall";
		// Creates a directory to look for files
		File root = pwd;
		// Creates file objects with file names
		File file = new File(root, blackList);
		File file2 = new File(root, whiteList);
		File file3 = new File(root, installedPackages);
		File file4 = new File(root, "active_processes.txt");
		// If the file does not exist, create it.
		if (!file.exists()) {
			CreateFile(blackList);
			Log.d(TAG, "black.txt not found. Creating file...");
		}
		if (!file2.exists()) {
			CreateFile(whiteList);
			Log.d(TAG, "white.txt not found. Creating file...");
		}
		if (!file3.exists()) {
			CreateFile(installedPackages);
			Log.d(TAG, "installed_programs.txt not found. Creating file...");
		}
		if (!file4.exists()) {
			CreateFile("active_processes.txt");
			Log.d(TAG, "active_processes.txt not found. Creating file...");
		}
	}

	public void RemoveStrFromFile(String blackorwhite, String packageName)
	// ////////////////////////////////////////////////////////////////////////////////////////
	// NAME: DoWork(String deleteFromFile)
	// AUTHOR: CDT Peter Lofreddo
	// LAST MODIFIED: 20 March 2012
	// DESCRIPTION: Deletes a String from a file
	// LIMITATIONS: Search Queries cast to lowercase for ease of use.
	// CITATIONS: Adapted from http://stackoverflow.com/questions/1377279/
	// java-find-a-line-in-a-file-and-remove
	//
	// ////////////////////////////////////////////////////////////////////////////////////////
	{
		// assumes black_list.txt or white_list.txt is
		// already created in mnt/sdcard/Nullwall directory
		String TAG = "FileOp_RemoveStrFromFile";
		try {
			// Makes sure programmer uses black or white as argument for method.
			if (!blackorwhite.equalsIgnoreCase("black")) {
				if (!blackorwhite.equalsIgnoreCase("white")) {
					Log.e("TAG", TAG
							+ " must have white or black as an argument");
				}
			}

			// Creates a new File object for the file as well as a temp object
			// to store the contents in while the method works.
			File root = new File(Environment.getExternalStorageDirectory(),
					"Nullwall");
			File file = new File(root, blackorwhite);
			File temp = new File(root, "temp.txt");

			// Creates buffers used for the File I/O
			BufferedReader bufferedReader = new BufferedReader(new FileReader(
					file.getAbsolutePath()));
			PrintWriter printerWriter = new PrintWriter(new FileWriter(
					temp.getAbsolutePath()));

			String line = null;
			// Read from the original file and write to the new
			// unless content matches data to be removed.
			while ((line = bufferedReader.readLine()) != null) {
				if (!line.trim().equalsIgnoreCase(packageName) & !line.trim().equalsIgnoreCase("\n")) {
					printerWriter.println(line.trim());
					printerWriter.flush();
				}
			}
			// Close the Buffers
			printerWriter.close();
			bufferedReader.close();

			// Delete the original file
			if (!file.delete()) {
				Log.e(TAG, "Could not delete file, error RemoveFromList()");
				return;
			}

			// Rename the new file to the filename the original file had.
			if (!temp.renameTo(file))
				Log.e(TAG, " Could not rename file, error " + TAG);

		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
			Log.e(TAG, "File Not Found, error " + TAG);

		} catch (IOException ex) {
			ex.printStackTrace();
			Log.e(TAG, "File Not Found, IO Exception " + TAG);
		}

	}

	public void UninstallApplication(String packageName)

	// ////////////////////////////////////////////////////////////////////////////////////////
	// NAME: UninstallApplication(String packageName)
	// AUTHOR: CDT Peter Lofreddo
	// LAST MODIFIED: 02 February 2012
	// DESCRIPTION:Uninstalls Package Permanently from Android.
	// LIMITATIONS: Must still ask user to permanently uninstall.
	// CITATIONS:Adapted from http://android.amberfog.com/?p=98
	//
	// ////////////////////////////////////////////////////////////////////////////////////////
	{
		Uri packageURI = Uri.parse("package:" + packageName);
		Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
		currentActivity.startActivity(uninstallIntent);
	}

	public void ListAllActivePrograms(TextView tv)
	// ////////////////////////////////////////////////////////////////////////////////////////
	// NAME: ListAllActivePrograms()
	// AUTHOR: CDT Peter Lofreddo
	// LAST MODIFIED: 23 February 2012
	// DESCRIPTION: Lists package names active on Android Device
	// LIMITATIONS: //
	// CITATIONS:adapted from http://www.dreamincode.net/forums/topic/
	// 138412-android-20-list-of-running-applications/
	//
	// ////////////////////////////////////////////////////////////////////////////////////////
	{
		PackageManager pm = currentActivity.getPackageManager();
		List<ApplicationInfo> packages = pm
				.getInstalledApplications(PackageManager.GET_META_DATA);
		ActivityManager actvityManager = (ActivityManager)

		currentActivity.getSystemService(currentActivity.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> procInfos = actvityManager
				.getRunningAppProcesses();
		tv.append("\n");
		for (int i = 0; i < procInfos.size(); i++) {
			tv.setText(tv.getText().toString() + procInfos.get(i).processName
					+ "\r\n");
		}
	}

	public void ListAllInstalledPrograms()
	// ////////////////////////////////////////////////////////////////////////////////////////
	// NAME: ListAllInstalledPrograms()
	// AUTHOR: CDT Peter Lofreddo
	// LAST MODIFIED: 26 February 2012
	// DESCRIPTION: Lists package names installed on Android Device
	// LIMITATIONS: Only Shows Apps installed by user. No System Apps (phone,
	// mms, etc.)
	// CITATIONS:http://stackoverflow.com/questions/2695746/
	// how-to-get-a-list-of-installed-android-applications-and-pick-one-to-run
	//
	// http://stackoverflow.com/questions/2695746/
	// how-to-get-a-list-of-installed-android-applications-and-pick-one-to-run
	//
	// ////////////////////////////////////////////////////////////////////////////////////////
	{
		//ArrayList<String> programList = null; 
		final PackageManager pm = currentActivity.getPackageManager();
		// Ensures the installed_programs.txt file is up to date each time list
		// is populated
		this.ClearFile(installedPackages);
		numberLinesInPrograms = 0;
		// get a list of installed app objects.
		List<ApplicationInfo> packages = pm
				.getInstalledApplications(PackageManager.GET_META_DATA);
		// Looks through all the packages currently installed on the phone
		for (ApplicationInfo packageInfo : packages) {
			// Checks to see if the package is a native system package
			if ((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
				// The Package is a System Package, Not showing, Uncomment below
				// line to display system packages
				// tv.append("System Package!" + "\n");
			} else {
				// Not a System Package. Showing.
				try {
					// Gives PackageNames instead of Simple Names, Uncomment
					// below to display (ex. com.android.pinball)
					// tv.append(packageInfo.packageName + "\n");

					// Gives Simple Names of Packages
					CharSequence c = pm.getApplicationLabel(pm
							.getApplicationInfo(packageInfo.packageName,
									PackageManager.GET_META_DATA));
					// appends line to installed_programs.txt
					//programList.add(packageInfo.packageName);
					this.AppendFile(packageInfo.packageName,
							installedPackages);
					
				} catch (Exception e) {
					Log.e("ListAllInstalledPrograms()",
							"Name Not Found Exception");
				}
			}
		}
		//return programList;
	}

	public void checkExternalMedia() {
		// ////////////////////////////////////////////////////////////////////////////////////////
		// NAME: checkExternalMedia()
		// AUTHOR: CDT Peter Lofreddo
		// LAST MODIFIED: 13 February 2012
		// DESCRIPTION: Determines whether external media is available and
		// writable. Useful for making sure USB Storage Mode is disabled and
		// file can be written. For Debugging purposes only at this point.
		// CITATIONS: Adapted from
		// http://eagle.phys.utk.edu/guidry/android/writeSD.html
		//
		// ///////////////////////////////////////////////////////////////////////////////////////
		boolean mExternalStorageAvailable = false;
		boolean mExternalStorageWriteable = false;
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
			// Can read and write the media
			mExternalStorageAvailable = mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			// Can only read the media
			mExternalStorageAvailable = true;
			mExternalStorageWriteable = false;
		} else {
			// Can't read or write
			mExternalStorageAvailable = mExternalStorageWriteable = false;
		}
		Log.d("Testing_checkExternalMedia", "\n\nExternal Media: readable="
				+ mExternalStorageAvailable + " writable="
				+ mExternalStorageWriteable);
	}

	public void ReadLocalResourceFile() {
		// ////////////////////////////////////////////////////////////////////////////////////////
		// NAME: ReadFileFromResRaw()
		// AUTHOR: CDT Peter Lofreddo
		// LAST MODIFIED: 29 January 2012
		// DESCRIPTION: Reads a file from Res/Raw directory of application.
		// Directory is READONLY
		// LIMITATIONS: Does not read all files on the filesystem.
		// CITATIONS: Adapted from
		// http://eagle.phys.utk.edu/guidry/android/writeSD.html
		//
		// ////////////////////////////////////////////////////////////////////////////////////////
		tv.append("\nData read from res/raw/textfile.txt:");
		InputStream is = currentActivity.getResources().openRawResource(
				R.raw.textfile);
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(
				new InputStreamReader(currentActivity.getResources()
						.openRawResource(R.raw.textfile)), 8192);
		try {
			String test;
			while (true) {
				test = br.readLine();
				// readLine() returns null if no more lines in the file
				if (test == null)
					break;
				tv.append("\n" + "    " + test);
			}
			isr.close();
			is.close();
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		tv.append("\n\n EOF");
	}

	public void CreateFile(String fileName)
	// ////////////////////////////////////////////////////////////////////////////////////////
	// NAME: CreateFile(String fileName)
	// AUTHOR: CDT Peter Lofreddo
	// LAST MODIFIED: 20 March 2012
	// DESCRIPTION: Creates a blank file in the Nullwall directory.
	// AppendFile(String sBody, String fileName). It has been added to provide
	// clarity an intuitive codebase, does not need CreateLogFile() to
	// first append to file, Used for clarity
	//
	// CITATIONS: Adapted from http://stackoverflow.com/questions/4580683/
	// android-java-writing-text-file-to-sd-card
	//
	// ////////////////////////////////////////////////////////////////////////////////////////
	{
		String TAG = "FileOp_CreateFile";
		try {
			// Makes new root object
			File root = new File(Environment.getExternalStorageDirectory(),
					"Nullwall");
			if (!root.exists()) {
				root.mkdirs();
			}
			// Starts writers
			File gpxfile = new File(root, fileName);
			FileWriter writer = new FileWriter(gpxfile, true);
			// Creates a file with nothing in it.
			writer.write("");
			// Closes writers
			writer.flush();
			writer.close();
			Log.d(TAG, fileName + " Successfully Created!");
		} catch (IOException e) {
			Log.e(TAG, fileName + " not Saved! CreateFile() Broken!");
		}
	}

	public void AppendFile(String sBody, String fileName) {
		// ////////////////////////////////////////////////////////////////////////////////////////
		// NAME: AppendFile(String sBody, String fileName)
		// AUTHOR: CDT Peter Lofreddo
		// LAST MODIFIED: 13 February 2012
		// DESCRIPTION: Adds text to a file located in the Nullwall Directory
		// does not need CreateLogFile() to first append to file; Used for
		// clarity
		// CITATIONS: Adapted from http://stackoverflow.com/questions/4580683/
		//
		// android-java-writing-text-file-to-sd-card
		//
		// ////////////////////////////////////////////////////////////////////////////////////////
		String TAG = "FileOp_AppendFile";
		try {
			File root = new File(Environment.getExternalStorageDirectory(),
					"Nullwall");
			if (!root.exists()) {
				root.mkdirs();
			}
			Log.d(TAG, "Log File Created In Directory: " + root);
			File gpxfile = new File(root, fileName);
			FileWriter writer = new FileWriter(gpxfile, true);
			writer.write(sBody + "\r\n");
			// Keeps track of the number of lines in the file
			if (fileName == blackList) {
				numberLinesInBlack = numberLinesInBlack + 1;
			} else if (fileName == whiteList) {
				numberLinesInWhite = numberLinesInWhite + 1;
			} else if (fileName == installedPackages) {
				numberLinesInPrograms += 1;
			} else if (fileName == "active_processes.txt") {
				numberLinesInActive = numberLinesInActive + 1;
			}

			// Closes writers
			writer.flush();
			writer.close();
			Log.d("FileOp_AppendFile", fileName
					+ " File Successfully Appended!");
		} catch (IOException e) {
			Log.e(TAG, fileName + " not Saved! AppendFile() Broken!");
		}
	}

	public void AppendSentenceToFile(String sBody, String fileName) {
		// ////////////////////////////////////////////////////////////////////////////////////////
		// NAME: AppendSentenceToFile(String sBody, String fileName)
		// AUTHOR: CDT Riley McKenna
		// LAST MODIFIED: 6 March 2012
		// DESCRIPTION: Adds text to a file located in the Nullwall Directory
		// does not need CreateLogFile() to first append to file; Used for
		// clarity
		// CITATIONS: Adapted from http://stackoverflow.com/questions/4580683/
		// //
		// android-java-writing-text-file-to-sd-card //
		// //
		// ////////////////////////////////////////////////////////////////////////////////////////
		try {
			File root = new File(Environment.getExternalStorageDirectory(),
					"Nullwall");
			if (!root.exists()) {
				root.mkdirs();
			}
			Log.d("FileOp_AppendFile", "Log File Created In Directory: " + root);
			File gpxfile = new File(root, fileName);
			FileWriter writer = new FileWriter(gpxfile, true);
			writer.write(sBody.toLowerCase());
			if (fileName == blackList) {
				numberLinesInBlack = numberLinesInBlack + 1;
			} else if (fileName == whiteList) {
				numberLinesInWhite = numberLinesInWhite + 1;
			} else if (fileName == installedPackages) {
				numberLinesInPrograms = numberLinesInPrograms + 1;
			} else if (fileName == "active_processes.txt") {
				numberLinesInActive = numberLinesInActive + 1;
			}

			writer.flush();
			writer.close();
			Log.d("FileOp_AppendFile", fileName
					+ " File Successfully Appended!");
		} catch (IOException e) {
			Log.d("FileOp_AppendFile", fileName
					+ " not Saved! AppendFile() Broken!");
		}
	}

	public void ReadFileTextView(String fileName, TextView tv1) {
		// ////////////////////////////////////////////////////////////////////////////////////////
		// NAME: ReadFileTextView(String fileName, TextView tv1)
		// AUTHOR: CDT Peter Lofreddo
		// LAST MODIFIED: 20 March 2012
		// DESCRIPTION: Reads a file from mnt/sdcard/Nullwall, displays using
		// textView
		// CITATIONS: Adapted from http://www.anddev.org/read_a_txt_file_
		// from_sd_card_in_a_real_device-t6619.html
		//
		// ////////////////////////////////////////////////////////////////////////////////////////
		String TAG = "FileOp_ReadFile";
		try {
			File f = new File(Environment.getExternalStorageDirectory()
					+ "/Nullwall/" + fileName);
			FileInputStream fileIS = new FileInputStream(f);
			BufferedReader buf = new BufferedReader(new InputStreamReader(
					fileIS));
			String readString = new String();
			Log.d("FileOp_ReadFile", "Reading File...");
			// just reading each line and pass it on the debugger
			while ((readString = buf.readLine()) != null) {
				tv1.append(readString);
				tv1.append("\r\n");
			}
		} catch (FileNotFoundException e) {
			Log.e(TAG, "File Not Found, error " + TAG);
			e.printStackTrace();
		} catch (IOException e) {
			Log.e(TAG, "IOException, error " + TAG);
			e.printStackTrace();
		}
	}

	public void ClearFile(String fileName) {
		// ////////////////////////////////////////////////////////////////////////////////////////
		// NAME: ClearFile(String fileName)
		// AUTHOR: CDT Peter Lofreddo
		// LAST MODIFIED: 20 March 2012
		// DESCRIPTION: Clears a file located in Nullwall Directory. Does not
		// Delete File.
		// CITATIONS: N/A
		//
		// ////////////////////////////////////////////////////////////////////////////////////////
		String TAG = "FileOp_ClearFile";
		try {
			// Creates new file object
			File root = new File(Environment.getExternalStorageDirectory(),
					"Nullwall");
			if (!root.exists()) {
				root.mkdirs();
			}
			// creates writers
			File gpxfile = new File(root, fileName);
			// Key difference here is the false boolean below. This makes append
			// false and overwrite by definition to true. We then overwrite the
			// file with "" and Boom goes the dynomite.
			FileWriter writer = new FileWriter(gpxfile, false);
			writer.write("");
			writer.flush();
			writer.close();
		} catch (FileNotFoundException e) {
			Log.e(TAG, "File not Found, error " + TAG);

		} catch (IOException e) {
			Log.e(TAG, "IOException, error " + TAG);
		}
	}

	public void DeleteFile(String fileName)
	// ////////////////////////////////////////////////////////////////////////////////////////
	// NAME: DeleteFile(String fileName)
	// AUTHOR: CDT Peter Lofreddo
	// LAST MODIFIED: 13 February 2012
	// DESCRIPTION: Deletes a file in the Nullwall Directory
	// LIMITATIONS: Must be in Nullwall Directory
	//
	// ////////////////////////////////////////////////////////////////////////////////////////
	{
		File root = new File(Environment.getExternalStorageDirectory(),
				"Nullwall");
		File file = new File(root, fileName);
		if (!file.delete()) {
			Log.e("FileOp_DeleteFile", fileName + " could not be deleted");
		}
	}

	public boolean SearchFile(String searchString, String fileName,
			Boolean caseSensitive) {
		// ////////////////////////////////////////////////////////////////////////////////////////
		// NAME: SearchFile(String searchString,String fileName, Boolean
		// caseSensitive)
		// AUTHOR: CDT Peter Lofreddo
		// LAST MODIFIED: 13 February 2012
		// DESCRIPTION: Searches in file for string in Nullwall Directory.
		// caseSensitive = true for Case sensitive
		// searching, choose false to ignore case
		// CITATIONS: Adapted From
		// http://stackoverflow.com/questions/5912753/java-find-and-replace-text
		//
		// ////////////////////////////////////////////////////////////////////////////////////////

		// Boolean flag which will switch to true once the string has been
		// found.`
		boolean found = false;
		String TAG = "FileOp_SearchFile";
		try {

			// Creates new file object
			File f = new File(Environment.getExternalStorageDirectory()
					+ "/Nullwall/" + fileName);
			// Starts streams, but dont cross them!
			FileInputStream fileIS = new FileInputStream(f);
			BufferedReader buf = new BufferedReader(new InputStreamReader(
					fileIS));
			String line;
			Log.v(TAG, buf + " :buffer");
			// Loop until end of file
			while ((line = buf.readLine()) != null) {
				//line = buf.readLine() + "";
				// checks to see if you want the term to be found if the case is
				// not correct
				if (caseSensitive) {
					// if the line has your search string (case sensitive),
					// switch flag to true
					if (line.equals(searchString)) {
						found = true;
						Log.d("FileOp_SearchFile", "File Found! Case Sensitive");
						return found;
					}
				} else if (line.equalsIgnoreCase(searchString)) {
					// you don't care about the case of the string (aka your
					// desperate)
					found = true;
					Log.d(TAG, searchString + " :File Found! Not Case Sensitive");
					return found;
				}
			}
			if (!found) {
				Log.e(TAG, "You did not find " + searchString + " in "
						+ fileName);
			}
			return found;
		} catch (FileNotFoundException e) {
			Log.e(TAG, "File not Found! SearchFile() Broken!");
			return true;

		} catch (IOException e) {
			Log.e(TAG, "IOException! SearchFile() Broken!");
			return true;
		}
	}

	public String[] ReadFileAsList(String fileName) {
		// ////////////////////////////////////////////////////////////////////////////////////////
		// NAME: ReadFileAsList(String fileName)
		// AUTHOR: CDT Peter Lofreddo, CDT Riley Mckenna
		// LAST MODIFIED: 20 March 2012
		// DESCRIPTION: Views file as a clickable ListView
		// LIMITATIONS: Search Queries cast to lowercase for ease of use.
		// CITATIONS: Adapted from http://stackoverflow.com/questions/8526831/
		// how-to-display-data-from-a-text-file-in-a-listview
		//
		// ////////////////////////////////////////////////////////////////////////////////////////
		String TAG = "FileOp_ReadFileAsList";
		try {
			// Creates File Object
			File f = new File(Environment.getExternalStorageDirectory()
					+ "/Nullwall/" + fileName);
			// Creates Readers
			FileReader file = new FileReader(f);
			BufferedReader buf = new BufferedReader(file);
			StringBuilder DbLines = new StringBuilder();
			List<String> lines = new ArrayList<String>();
			String line = null;

			// Uses the number of lines to properly show the list view.
			if (fileName == blackList) {
				String[] lineText = new String[numberLinesInBlack];

				for (int i = 0; i < numberLinesInBlack; i++) {
					lineText[i] = (buf.readLine());
				}
				for (int i = 0; i < numberLinesInBlack; i++) {
					System.out.println(lineText[i] + "###");
					DbLines.append(lineText[i]);
					DbLines.append("\r\n");
				}
			} else if (fileName == whiteList) {
				String[] lineText = new String[numberLinesInWhite];

				for (int i = 0; i < numberLinesInWhite; i++) {
					lineText[i] = (buf.readLine());
				}
				for (int i = 0; i < numberLinesInWhite; i++) {
					System.out.println(lineText[i] + "###");
					DbLines.append(lineText[i]);
					DbLines.append("\r\n");
				}
			} else if (fileName == installedPackages) {

				while ((line = buf.readLine()) != null){
					lines.add(line);
				}
				buf.close();
				return lines.toArray(new String[lines.size()]);
			} else if (fileName == "active_processes.txt") {
				String[] lineText = new String[numberLinesInActive];

				for (int i = 0; i < numberLinesInActive; i++) {
					lineText[i] = (buf.readLine());
				}
				for (int i = 0; i < numberLinesInActive; i++) {
					System.out.println(lineText[i] + "###");
					DbLines.append(lineText[i]);
					DbLines.append("\r\n");
				}
			}

			// Split is used to seperate the list items by a new line so that
			// the list does not populate into one big Array of 1 list.
			String[] ListItems = DbLines.toString().split("\r\n");

			return ListItems;
		}

		catch (FileNotFoundException e) {
			Log.e(TAG, "File Not Found in ReadFileAsList()");

		} catch (IOException e) {
			Log.e(TAG, "IOException in ReadFileAsList()");
		}
		Log.e(TAG, "Probably an Error in ReadFileAsList()");
		return null;

	}

	public String readFileLine(String fileName, int index) {
		// ////////////////////////////////////////////////////////////////////////////////////////
		// NAME: readFileLine(String fileName, int index)
		// AUTHOR: CDT Riley Mckenna
		// LAST MODIFIED: 20 March 2012
		// DESCRIPTION: Reads a single line of file given a file name and a
		// specified index
		// LIMITATIONS: Can only read one line, cannot read ranges
		// CITATIONS: Adapted from http://stackoverflow.com/questions/8526831/
		// how-to-display-data-from-a-text-file-in-a-listview
		//
		// ////////////////////////////////////////////////////////////////////////////////////////
		String TAG = "FileOp_ReadFileAsList";
		try {
			File f = new File(Environment.getExternalStorageDirectory()
					+ "/Nullwall/" + fileName);
			System.out.println(f);
			FileReader file;

			file = new FileReader(f);

			System.out.println(file);
			BufferedReader buf = new BufferedReader(file);
			System.out.println(buf);
			StringBuilder DbLines = new StringBuilder();

			if (fileName == blackList) {
				String[] lineText = new String[numberLinesInBlack];

				for (int i = 0; i < numberLinesInBlack; i++) {
					lineText[i] = (buf.readLine());
				}
				return lineText[index];
			} else if (fileName == whiteList) {
				String[] lineText = new String[numberLinesInWhite];

				for (int i = 0; i < numberLinesInWhite; i++) {
					lineText[i] = (buf.readLine());
				}
				return lineText[index];
			} else if (fileName == installedPackages) {
				String[] lineText = new String[this.getNumberLinesInFile(fileName)];

				for (int i = 0; i < this.getNumberLinesInFile(fileName); i++) {
					lineText[i] = (buf.readLine());
				}
				return lineText[index];
			} else if (fileName == "active_processes.txt") {
				String[] lineText = new String[numberLinesInActive];

				for (int i = 0; i < numberLinesInActive; i++) {
					lineText[i] = (buf.readLine());
				}
				return lineText[index];
			}

		} catch (FileNotFoundException e) {
			Log.e(TAG, "File Not Found, error in ReadFileAsList()");
		} catch (IOException e) {
			e.printStackTrace();
			Log.e(TAG, "IOException in ReadFileAsList()");
		}
		Log.e(TAG, "Probably an Error in ReadFileAsList()");
		return null;
	}

	public String getBlackList() {
		return blackList;
	}

	public void setBlackList(String blackList) {
		this.blackList = blackList;
	}

	public String getWhiteList() {
		return whiteList;
	}

	public void setWhiteList(String whiteList) {
		this.whiteList = whiteList;
	}

	public String getInstalledPackages() {
		return installedPackages;
	}

	public void setInstalledPackages(String installedPackages) {
		this.installedPackages = installedPackages;
	}

}
